package sgi.client;

import java.io.IOException;
import java.util.Iterator;


import sgi.details_new.ImageDetails;

import sgi.message.MessageImageDelete;
import sgi.message.MessageImageRemoving;

public class SGIImageDeleteController extends SGIController {

	public static SGIImageDeleteController deleteReview;
	public static SGIImageDeleteUI deleteReviewUI;
	/** constructor */
	public SGIImageDeleteController() {
		SGIImageDeleteController.deleteReviewUI = new SGIImageDeleteUI();
	}


	/** create controller  */
	public static SGIImageDeleteController create() throws IOException
	{
		if(SGIImageDeleteController.deleteReview == null)
		{
			SGIImageDeleteController.deleteReview = new SGIImageDeleteController();
			deleteReviewUI.setImageDeleteController(SGIImageDeleteController.deleteReview);
		}
		SGIImageDeleteController.deleteReviewUI.setVisible(true);
		return deleteReview;
	}
	
	/** terminating the UI */
	public static void terminate()
	{
		SGIImageDeleteController.deleteReviewUI.setVisible(false);
		SGIImageDeleteController.deleteReview=null;
		SGIImageDeleteController.deleteReviewUI.setVisible(false);
		SGIImageDeleteController.deleteReviewUI=null;
	}
	/** sending message to get the table of images we want to select to delete */
	public void DeleteReview() {
		MessageImageDelete msg = new MessageImageDelete();
		sendImageDelete(msg);
		
	}

	private void sendImageDelete(MessageImageDelete msg) {
		try {
			(SGIClientConnectionController.createConnection()).handleMessageFromClientUI(msg);
		} catch  (IOException e) {e.printStackTrace();}
	}
	/** sending message of selected column of images for deletion  */
	public void MessageFromImageDeleteUI(int imageid) {
		MessageImageRemoving msg = new MessageImageRemoving(imageid);
		try {
				SGIClientConnectionController conn = SGIClientConnectionController.createConnection();
				conn.handleMessageFromClientUI(msg);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
	
	/** handle message for inserting the images we want to select to the table */
	@Override
	void handleMessage(Object msg) {
		Iterator<ImageDetails> iterator = (((MessageImageDelete)msg).getimageDetails()).iterator();
		SGIImageDeleteController.deleteReviewUI.clearTable();
		while (iterator.hasNext()) {
			SGIImageDeleteController.deleteReviewUI.addRows(iterator.next());
		}
	}
	/** getting the UI  */
	public static SGIImageDeleteUI getUI()
	{
		return deleteReviewUI;
	}

}
